
#include "cell.h"
#include "trunk.h"
#include <thread>
#include <mutex>

namespace O1 {
	namespace Storage {
		namespace Memory {

			using sync_lock = std::lock_guard<std::mutex>;

			static std::mutex trunk_lock;

			void trunk::initialize(long long size) {
				if (this->store == nullptr) {
					this->store = new cell_store();
				}

				this->store->reserve(size);
			};

			void trunk::clear() {
				auto s = this->store;
				if (s != nullptr) {
					store = nullptr;
					s->clear();
					delete s;
				}
			};

			long long trunk::add_cell(byte* const &value, const int sz) {
				long long index = -1;
				{
					sync_lock lock (trunk_lock);
					index = this->store->size();
					this->store->emplace_back(cell(value, sz));
				}

				return index;
			};
		}
	}
}
